import 'package:fish_redux/fish_redux.dart';
import '../../../app_files.dart';
import 'action.dart';
import 'state.dart';

Effect<BindAccountViewState> buildEffect() {
  return combineEffects(<Object, Effect<BindAccountViewState>>{
    BindAccountViewAction.action: _onAction,
    BindAccountViewAction.onSubmit: _onSubmit,
  });
}

void _onAction(Action action, Context<BindAccountViewState> ctx) {}

void _onSubmit(Action action, Context<BindAccountViewState> ctx) async {
  if (ctx.state.accountController.text.length == 0) {
    CommonUtils.showErrorInfo(ctx.context, '请输入户号');
    return;
  }
  if (ctx.state.nameController.text.length == 0) {
    CommonUtils.showErrorInfo(ctx.context, '请输入户主姓名');
    return;
  }
  CommonUtils.closeKey(ctx.context);
  CommonUtils.showLoadingDialog(ctx.context);
  ResultData res = await CommonNetWork.gasNoBlind(
    userName: ctx.state.nameController.text,
    accountNo: ctx.state.accountController.text,
  );
  Navigator.pop(ctx.context);
  if (res.success) {
    bus.emit('refreshAccountList');
    await CommonUtils.showAlertView(ctx.context, '绑定成功');
    Navigator.pop(ctx.context);
  } else {
    CommonUtils.showErrorInfo(ctx.context, res.desc);
  }
}
